package com.cskaoyan.day01.linkdemo;

/**
 * 遍历链表
 * @author stone
 * @date 2022/01/27 14:37
 */
public class LinkedDemo1 {

    public static void main(String[] args) {
        Node node1 = new Node("红");
        Node node2 = new Node("橙");
        Node node3 = new Node("黄");
        Node node4 = new Node("绿");
        Node node5 = new Node("青");
        Node node6 = new Node("蓝");
        Node node7 = new Node("紫");

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        //红 → 橙 → 黄 → 绿 → 青 → 蓝 → 紫
        show(node1);
    }

    //head头 rear屁股
    public static void show(Node head) {
        if (head == null) {
            System.out.println("链表为空");//抛出异常也行
            return;
        }
        //需要一个额外的标记 → 标记当前结点
        Node current = head;
        /*while (true) {
            //如果current.next ，没有后继结点就停止遍历
            if (current.next ==null){
                break;
            }
            System.out.print(current.value + "-->");
            //如果有后继结点，current后移
            current = current.next;
        }
        System.out.println(current.value);*/
        /*while (current.next != null) {
            System.out.print(current.value + "-->");
            current = current.next;
        }
        System.out.println(current.value);*/
        while (current != null) {
            System.out.print(current.value + "-->");
            current = current.next;
        }
    }
}
